<!-- 添加考试 -->
<template>
  <div>
    <el-form ref="form" :model="form" label-width="100px">
      <el-form-item
        label="难度"
        size="small"
        style="width: 400px; overflow: hidden"
      >
        <el-select
          v-model="difficultyValue"
          placeholder="试题难度"
          class="w150"
          style="display: block; width: 300px; height: 50px"
        >
          <el-option
            style="height: 45px; line-height: 45px"
            v-for="item in difficulty"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="试卷名称" style="width: 400px">
        <el-input v-model="paper.papername"></el-input>
      </el-form-item>
      <el-form-item label="考试科目" style="width: 400px">
        <el-input v-model="paper.subject"></el-input>
      </el-form-item>
      <el-form-item label="选择题数量" style="width: 400px">
        <el-input type="number" v-model="paper.multiNum"></el-input>
      </el-form-item>
      <el-form-item label="填空题数量" style="width: 400px">
        <el-input type="number" v-model="paper.fillNum"></el-input>
      </el-form-item>
      <el-form-item label="判断题数量" style="width: 400px">
        <el-input type="number" v-model="paper.judgeNum"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="open()">立即创建</el-button>
        <el-button type="text" @click="close()">取消</el-button>
      </el-form-item>
    </el-form>

    <!-- 模态框 -->
    <el-dialog
      title="编辑试卷信息"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <section class="update">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="试卷名称">
            <el-input v-model="form.paperName" disabled></el-input>
          </el-form-item>
          <el-form-item label="介绍">
            <el-input v-model="form.description"></el-input>
          </el-form-item>
          <el-form-item label="所属学院">
            <el-input v-model="form.institute"></el-input>
          </el-form-item>
          <el-form-item label="所属专业">
            <el-input v-model="form.major"></el-input>
          </el-form-item>
          <el-form-item label="考试科目">
            <el-input v-model="form.subject" disabled></el-input>
          </el-form-item>
          <el-form-item label="年级">
            <el-input v-model="form.grade"></el-input>
          </el-form-item>
          <el-form-item label="考试日期">
            <el-col :span="11">
              <el-date-picker
                placeholder="选择日期"
                v-model="form.examdate"
                style="width: 100%"
              ></el-date-picker>
            </el-col>
          </el-form-item>
          <el-form-item label="持续时间">
            <el-input v-model="form.totaltime"></el-input>
          </el-form-item>
          <el-form-item label="总分">
            <el-input v-model="form.totalscore" disabled></el-input>
          </el-form-item>
          <el-form-item label="考试类型">
            <el-input v-model="form.type"></el-input>
          </el-form-item>
          <el-form-item label="考生提示">
            <el-input type="textarea" v-model="form.tips"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submit()">立即创建</el-button>
            <el-button type="text" @click="cancel()">取消</el-button>
          </el-form-item>
        </el-form>
      </section>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      paper: {
        papername: null,
        subject: null,
        multiNum: null,
        fillNum: null,
        judgeNum: null,
      },
      dialogVisible: false, //对话框
      difficulty: [
        //试题难度
        {
          value: "简单",
          label: "简单",
        },
        {
          value: "一般",
          label: "一般",
        },
        {
          value: "困难",
          label: "困难",
        },
      ],
      difficultyValue: "简单",
      levels: [
        //难度等级
        {
          value: "1",
          label: "1",
        },
        {
          value: "2",
          label: "2",
        },
        {
          value: "3",
          label: "3",
        },
        {
          value: "4",
          label: "4",
        },
        {
          value: "5",
          label: "5",
        },
      ],
      form: {
        //表单数据初始化
        paperName: null,
        description: null, //介绍
        institute: null, //学院
        major: null, //专业
        subject: null,
        grade: null, //年级
        examdate: null, //考试日期
        totaltime: null, //持续时间
        totalscore: null, //总分
        type: null, //考试类型
        tips: null, //考生提醒
        // paperId: null,  //试卷id
      },
    };
  },
  methods: {
    open() {
      this.dialogVisible = true;
      this.form.paperName = this.paper.papername;
      this.form.totalscore =
        (Number(this.paper.multiNum) +
          Number(this.paper.fillNum) +
          Number(this.paper.judgeNum)) *
        2;
      this.form.subject = this.paper.subject;
      // let examDate = this.formatTime(this.form.examDate)
      // this.form.examDate = examDate.substr(0,10)
      // this.$axios(`/api/examManagePaperId`).then(res => {
      //   this.form.paperId = res.data.data.paperId + 1 //实现paperId自增1
      //   this.$axios({
      //     url: '/api/exam',
      //     method: 'post',
      //     data: {
      //       ...this.form
      //     }
      //   }).then(res => {
      //     if(res.data.code == 200) {
      //       this.$message({
      //         message: '数据添加成功',
      //         type: 'success'
      //       })
      //       this.$router.push({path: '/selectExam'})
      //     }
      //   })
      // })
    },
    submit() {
     
      let teacherid =  this.$cookies.get("cid");
      let paper = this.paper;
      let examManage = this.form;
       console.log(paper);
      console.log(examManage);
      this.$axios({
        url: "/api/addExam",
        method: "post",
        data: {
          paper,
          examManage,
          teacherid,
        },
      }).then((res) => {
        if (res.data.code == 200) {
          this.$message({
            message: "数据添加成功",
            type: "success",
          });
          this.$router.push({ path: "/teacherManage" });
        }
      });
    },
    close() {
      //取消按钮
      this.paper = {};
    },
    cancel() {
      //取消按钮
      // this.form = {};
      this.form.description = "";
      this.form.institute = "";
      this.form.major = "";
      this.form.grade = "";
      this.form.examDate = "";
      this.form.totalTime = "";
      this.form.type = "";
      this.form.tips = "";
      this.form.description = "";
      this.form.description = "";
    },
    handleClose(done) {
      //关闭提醒
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    formatTime(date) {
      //日期格式化
      let year = date.getFullYear();
      let month =
        date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1;
      let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
      let hours =
        date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
      let minutes =
        date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
      let seconds =
        date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      // 拼接
      return (
        year +
        "-" +
        month +
        "-" +
        day +
        " " +
        hours +
        ":" +
        minutes +
        ":" +
        seconds
      );
    },
  },
};
</script>
<style lang="scss" scoped>
.add {
  padding: 0px 40px;
  width: 400px;
}
input .el-input__inner {
  height: 45px;
  line-height: 45px;
}
</style>